$(document).ready(function () {
    validateUser();
});


var ipAddress="localhost";
var userName="";
//验证用户是否登录
var validateUser=function(){
    //layer.msg("okk");
    $.ajax({
        //url: "http://" + ipAddress + ":8080/user/get_user_info",
        url: "http://"+ipAddress+":8004/orderInfo/checkAccount",
        type: "GET",
        async: true,
        data: null,
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            console.log(" data====  " + data);
            if (data.message =="success") {
                userName=data.data.nikename;
                getMyOrderInfo(9);
            }else{
                layer.msg("请先登录！");
                setTimeout(function(){//1秒后跳转
                    window.location.href = "./login.html"
                },1000);
            }
        },
        error: function () {
            layer.msg("后台服务器开小差了...")
        }
    })
};

//加载页面信息
var getMyOrderInfo=function(obj){
    //layer.msg("obj===="+obj);
    $("#orderList").empty();
    $.ajax({
        //url: "http://" + ipAddress + ":8080/user/get_user_info",
        url: "http://" + ipAddress + ":8004/orderInfo/queryOrderByType/"+obj,
        type: "GET",
        async: true,
        data: null,
        contentType: "application/json",
        dataType: "json",
        success: function (data) {
            console.log(" data====  " + data);
            if (data.success ==true) {
                if(data.message=="暂时没有订单！"){
                    $("#orderList").append("<p class=\"empty\">当前没有交易订单。</p>")
                }else{
                    //先清空对象的所有子元素
                    $("#orderList").empty();

                    //再添加新元素
                    $.each(data.data,function (index,value) {
                        console.log("index==="+index);
                        //订单类型
                        var type="";
                        //付款提示
                        var payTip="";
                        //金额
                        var payment=0.0;
                        //支付方式
                        var payway="";

                        var operation="";
                        var operation1="                                                    <td class=\"order-actions\"><a class=\"btn btn-small btn-line-gray\"\n" +
                            "                                                                                 href='./orderDetail.html?orderNo="+value.orderInfo.orderNo+"' data-id='value.orderInfo.orderNo'>订单详情</a>\n" +
                            "                                                    </td>\n";

                        var operation2=
                        "                                                       <td class=\"order-actions\"><a class=\"btn btn-small btn-primary\" href=\"//order.mi.com/buy/confirm.php?id=5190511984908868\" target=\"_blank\" data-id='value.orderInfo.orderNo'>立即支付</a>" +
                        "                                                                                   <a class=\"btn btn-small btn-line-gray\" href='./orderDetail.html?orderNo="+value.orderInfo.orderNo+"' data-id='value.orderInfo.orderNo'>订单详情</a>           " +
                        "                                                       </td>";


                        switch (value.orderInfo.orderStatus) {
                            case 0:
                                type="待付款";
                                payTip="应付金额：";
                                payment=value.orderInfo.totalMerchandise+value.orderInfo.transferFee;
                                operation=operation2;
                                break;
                            case 1:
                                type="待发货";
                                payTip="订单总额：";
                                payment=value.orderInfo.actualPayment;
                                operation=operation1;
                                break;
                            case 2:
                                type="待收货";
                                payTip="订单总额：";
                                payment=value.orderInfo.actualPayment;
                                operation=operation1;
                                break;
                            case 3:
                                type="待评价";
                                payTip="订单总额：";
                                payment=value.orderInfo.actualPayment;
                                operation=operation1;
                                break;
                            case 4:
                                type="已完成";
                                payTip="订单总额：";
                                payment=value.orderInfo.actualPayment;
                                operation=operation1;
                                break;
                            case 5:
                                type="已关闭";
                                payTip="订单总额：";
                                payment=value.orderInfo.totalMerchandise+value.orderInfo.transferFee;
                                operation=operation1;
                                break;
                            default:
                                type="未知类型";
                        }

                        $("#orderList").append("<li class=\"uc-order-item uc-order-item-finish\">\n" +
                            "                                        <div class=\"order-detail\">\n" +
                            "                                            <div class=\"order-summary\">\n" +
                            "                                                <div class=\"order-status\">"+type+"</div>\n" +
                            "                                            </div>\n" +
                            "                                            <table class=\"order-detail-table\">\n" +
                            "                                                <thead>\n" +
                            "                                                <tr>\n" +
                            "                                                    <th class=\"col-main\"><p class=\"caption-info\">"+value.orderInfo.orderTime+"<span\n" +
                            "                                                            class=\"sep\">|</span>"+userName+"<span class=\"sep\">|</span>订单号： <a\n" +
                            "                                                            href=\"//order.mi.com/user/orderView/5171226185408617\">"+value.orderInfo.orderNo+"</a><span\n" +
                            "                                                            class=\"sep\">|</span>支付宝（APP支付）</p></th>\n" +
                            "                                                    <th class=\"col-sub\"><p class=\"caption-price\">"+payTip+"<span class=\"num\">"+payment+"</span>元\n" +
                            "                                                    </p></th>\n" +
                            "                                                </tr>\n" +
                            "                                                </thead>\n" +
                            "                                                <tbody>\n" +
                            "                                                <tr>\n" +
                            "                                                    <td class=\"order-items\">\n" +
                            "                                                        <ul class='goods-list' id='goodsList"+index+"'>\n" +

                            "                                                        </ul>\n" +
                            "                                                    </td>\n" +
                            // "                                                    <td class=\"order-actions\"><a class=\"btn btn-small btn-line-gray\"\n" +
                            // "                                                                                 href=\"//order.mi.com/user/orderView/5171226185408617\">订单详情</a>\n" +
                            // "                                                    </td>\n" +
                            // "                                                       <td class=\"order-actions\"><a class=\"btn btn-small btn-primary\" href=\"//order.mi.com/buy/confirm.php?id=5190511984908868\" target=\"_blank\">立即支付</a>" +
                            // "                                                                                   <a class=\"btn btn-small btn-line-gray\" href=\"//order.mi.com/user/orderView/5190511984908868\">订单详情</a>           " +
                            // "                                                       </td>"+
                            operation+
                            "                                                </tr>\n" +
                            "                                                </tbody>\n" +
                            "                                            </table>\n" +
                            "                                        </div>\n" +
                            "                                    </li>");
                        $.each(value.orderProductVOTwoList,function (productIndex,value) {
                            var appendId="goodsList"+index;

                            console.log("appendId=="+appendId);
                            $("#goodsList"+index).append("<li>\n" +
                                "                                                                <div class=\"figure figure-thumb\"><a\n" +
                                "                                                                        href=\"//item.mi.com/1171600001.html\"\n" +
                                "                                                                        target=\"_blank\"> <img\n" +
                                "                                                                        src="+value.goodsVO.picture+"            \n" +
                                "                                                                        width=\"80\" height=\"80\" alt=\""+value.orderProduct.goodsName+"\">\n" +
                                "                                                                </a></div>\n" +
                                "                                                                <p class=\"name\"><a target=\"_blank\"\n" +
                                "                                                                                   href='./goodsDetail.html?keywords="+value.goodsVO.goodsName+"'>"+value.orderProduct.goodsName+"\n" +
                                "                                                                    </a></p>\n" +
                                "                                                                <p class=\"price\"> <span id=''>"+value.orderProduct.goodsPrice+"</span>元 × <span id=''>"+value.orderProduct.goodsNumber+"</span> </p>\n" +
                                "                                                            </li>")
                        });
                    });
                }
            }else{
                if (data.message=="请先登录！") {
                    layer.msg("请先登录！");
                    setTimeout(function(){//1秒后跳转
                        window.location.href = "./login.html"
                    },2000);
                }else{
                    layer.msg(""+data.message);
                }
            }
        },
        error: function () {
            layer.msg("后台服务器开小差了...")
        }
    })

}



//选择订单类型,并查询订单
var chooseOrderType=function(obj){
    $(obj).css("color","orangered");
    $.each($(obj).parent().siblings(),function (index,value) {
        $(value).children("a").css("color","grey");
    });

    $("#orderList").empty();
    var type=$(obj).attr("data-type");
    getMyOrderInfo(type);
};





























